package com.tianqu.tms.www.cardect.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.action.CarWaitConfirmTopListAction;
import com.tianqu.tms.www.cardect.viewhelper.CarWaitConfirmViewHelper;

public class CarWaitConfirmTopListController extends TmsController {

	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			TmsDetectInfo car = TmsDetectInfo.getInstance();
			if (uc.getAttribute(CarWaitConfirmTopListAction.PAGENO) != null) {
				car.setPageNo(Integer.parseInt((String) uc
						.getAttribute(CarWaitConfirmTopListAction.PAGENO)));
			}
			if (uc.getAttribute(CarWaitConfirmTopListAction.MAXINDEX) != null) {
				car.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(CarWaitConfirmTopListAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarWaitConfirmTopListAction.SORTKEY) != null) {
				car.setSortKey(Integer.parseInt((String) uc
						.getAttribute(CarWaitConfirmTopListAction.SORTKEY)));
			}
			if (uc.getAttribute(CarWaitConfirmTopListAction.ASC) != null) {
				car.setAsc((new Boolean((String) uc
						.getAttribute(CarWaitConfirmTopListAction.ASC)))
						.booleanValue());
			}

			DectCodes code = DectCodes.getInstance();
			// 从tis_car_info表中取值放到一个list中供前台调用

			List<TmsDetectInfo> list = car.getAllocationCarInfoBySource("",
					TmsDetectInfo.WAIT_TOBE_CONFIRMED);
			List<CarWaitConfirmViewHelper> viewList = new ArrayList<CarWaitConfirmViewHelper>();
			CarWaitConfirmViewHelper view = new CarWaitConfirmViewHelper();
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					TmsDetectInfo entity = (TmsDetectInfo) list.get(i);
					// 将entity数据转型放到viewhelp中
					view = new CarWaitConfirmViewHelper();
					view = setViewFromEntity(entity, view);
					// 设置页面检测模式显示
					view.setChkModel(code.getItemValueByItemIdAndItemCode(
							DectCodes.DETECT_MODE, String.valueOf(entity
									.getChkModel())));
					viewList.add(view);
				}
			}

			view.setWaitConfirmList(viewList);

			uc.setAttribute(TmsConstants.VIEWHELP, view);
			// uc.removeAttribute(SOURSE);//每次执行都remove掉来源
			uc.setAttribute(PAGING, car);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 重构下list 将数据库数据转化成页面需要的数据
	 * 
	 * @throws TmsDaoException
	 */
	private CarWaitConfirmViewHelper setViewFromEntity(TmsDetectInfo entity,
			CarWaitConfirmViewHelper view) throws TmsDaoException {
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setRegDate(entity.getRegDate());
		view.setCarCheckInsur(entity.getCarCheckInsur());
		view.setTrafficInsur(entity.getTrafficInsur());
		view.setColor(entity.getColor());
		view.setUpdateTime(entity.getUpdateTime());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));// 毫升转成升+“L”
		DectCodes code = DectCodes.getInstance();

		view.setGearbox(code.getItemValueByItemIdAndItemCode(
				DectCodes.GEARBOX_TYPE, String.valueOf(entity.getGearbox())));
		view.setFuelDict(code.getItemValueByItemIdAndItemCode(
				DectCodes.FUEL_TYPE, String.valueOf(entity.getFuelDict())));
		view.setNature(code.getItemValueByItemIdAndItemCode(
				DectCodes.NATURE_TYPE, String.valueOf(entity.getNature())));
		view.setTotalScore(entity.getTotalScore());// 总评
		view.setExternalScore(entity.getExternalScore());// 外装
		view.setInternalScore(entity.getInternalScore());// 内饰
		view.setMileage(entity.getMileage());

		view.setCssFlag(entity.getSeeBsflag());

		return view;
	}
}
